﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace S4Store.Models.AfterSaleReport
{
    public class AfterSaleCarModelHistoryModel : BaseReportModel
    {
        public List<ReportModel> AfterSaleCarTypeList { get; set; }
        public int BrandId { get; set; }

        public List<S4Store.Domain.AfterSaleKpi.AfterSaleCarTypeMonthConfig> CTMonths { get; set; }
        public List<S4Store.Domain.AfterSaleKpi.AfterSaleCarTypeDayInput> CTDays { get; set; }
        public S4Store.Domain.AfterSaleKpi.MonthStandardConfig StandardConfig { get; set; }

        public S4Store.Models.Chart.Pie TimeRate { get; set; }
        public class ReportModel
        {
            public DateTime Month { get; set; }
            public decimal 五K保养产值 { get; set; }
            public decimal 十K保养产值 { get; set; }
            public decimal 常态维修产值 { get; set; }
            
        }

        /// <summary>
        /// 
        /// </summary>
        public decimal _5K保养完成率(int brandid,int year,int month)
        {
            var _5K目标保养数 = CTMonths.Where(o => o.CarBrandId == brandid && o.Month.Year == year && o.Month.Month==month).Sum(o => o.BYYSS5KBYS_MonthTarget5K);
            var _5K实际保养数 = CTDays.Where(o => o.CarBrandId == brandid && o.AddTime.Year == year && o.AddTime.Month == month).Sum(o => o.WKBYS_Maintain5KNum);
            if (_5K目标保养数 == 0 || _5K实际保养数 == 0) return 0;
            return Math.Round((decimal)_5K实际保养数 / (decimal)_5K目标保养数 * 100, 1, MidpointRounding.AwayFromZero);
        }

        public decimal _10K保养完成率(int brandid, int year, int month)
        {
            var _10K目标保养数 = CTMonths.Where(o => o.CarBrandId == brandid && o.Month.Year == year && o.Month.Month == month).Sum(o => o.BYYSS10KBYS_MonthTarget10k);
            var _10K实际保养数 = CTDays.Where(o => o.CarBrandId == brandid && o.AddTime.Year == year && o.AddTime.Month == month).Sum(o => o.SQBYS_Maintain10KNum);
            if (_10K目标保养数 == 0 || _10K实际保养数 == 0) return 0;
            return Math.Round((decimal)_10K实际保养数 / (decimal)_10K目标保养数 * 100, 1, MidpointRounding.AwayFromZero);
        }

        public decimal 维修入场台次完成率(int brandid, int year, int month)
        {
            var 目标维修入场台次 = CTMonths.Where(o => o.CarBrandId == brandid && o.Month.Year == year && o.Month.Month == month).Sum(o => o.BYWXRCTCMB_MonthMaintainCarToFactoryTarget);
            var 实际维修入场台次 = CTDays.Where(o => o.CarBrandId == brandid && o.AddTime.Year == year && o.AddTime.Month == month).Sum(o => o.WXRCTC_MaintainCarToFactory);
            if (目标维修入场台次 == 0 || 实际维修入场台次 == 0) return 0;
            return Math.Round((decimal)实际维修入场台次 / (decimal)目标维修入场台次 * 100, 1, MidpointRounding.AwayFromZero);
        }

        public decimal 首次续保完成率(int brandid, int year, int month)
        {
            var 首次续保完成目标 = CTMonths.Where(o => o.CarBrandId == brandid && o.Month.Year == year && o.Month.Month == month).Sum(o => o.SCXBTCMB_FirstRenewTarget);
            var 实际续保完成目标 = CTDays.Where(o => o.CarBrandId == brandid && o.AddTime.Year == year && o.AddTime.Month == month).Sum(o => o.SCXB_FirstRenewNum);
            if (首次续保完成目标 == 0 || 实际续保完成目标 == 0) return 0;
            return Math.Round((decimal)实际续保完成目标 / (decimal)首次续保完成目标 * 100, 1, MidpointRounding.AwayFromZero);
        }

        public decimal 二次续保完成率(int brandid, int year, int month)
        {
            var 二次续保完成目标 = CTMonths.Where(o => o.CarBrandId == brandid && o.Month.Year == year && o.Month.Month == month).Sum(o => o.ECXBTCMB_SecondRenewTarget);
            var 实际二次续保完成 = CTDays.Where(o => o.CarBrandId == brandid && o.AddTime.Year == year && o.AddTime.Month == month).Sum(o => o.ECXB_SecondRenewNum);
            if (二次续保完成目标 == 0 || 实际二次续保完成 == 0) return 0;
            return Math.Round((decimal)实际二次续保完成 / (decimal)二次续保完成目标 * 100, 1, MidpointRounding.AwayFromZero);
        }

        public decimal 事故车台次完成率(int brandid, int year, int month)
        {
            var 事故车台次完成目标 = CTMonths.Where(o => o.CarBrandId == brandid && o.Month.Year == year && o.Month.Month == month).Sum(o => o.BYSGCRCTCMB_MonthAacidentCarToFactoryTarget);
            var 实际事故车台次完成 = CTDays.Where(o => o.CarBrandId == brandid && o.AddTime.Year == year && o.AddTime.Month == month).Sum(o => o.SGRCTC_AccidentCarToFactory);
            if (事故车台次完成目标 == 0 || 实际事故车台次完成 == 0) return 0;
            return Math.Round((decimal)实际事故车台次完成 / (decimal)事故车台次完成目标 * 100, 1, MidpointRounding.AwayFromZero);
        }
    }
}
